package elcs.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import elcs.member.model.MemberVO;
import elcs.mentoring.model.MentoringService;
import elcs.mentoring.model.NoteVO;

@Controller
public class MentoringController {
	@Resource(name="mentoringServiceImpl")
	private MentoringService service;

	
	@RequestMapping("mentoring/mentoring.do")
	public String mentoring(){
		return "mentoring.mentoring";
	}
	
	@RequestMapping("mentoring/status.do")
	public ModelAndView statusView(HttpSession session) throws Exception{
		MemberVO mvo=(MemberVO)session.getAttribute("mvo");
		return new ModelAndView("status.mentoring","statusMap",service.statusView(mvo.getId()));
	}

	/**
	 * Mentoring > Question > 멘토에게 질문하기 가능
	 * @SSA
	 * @param session
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/mentoring/question.do")
	public ModelAndView questionView(HttpSession session) throws Exception{
		MemberVO mvo=(MemberVO)session.getAttribute("mvo");
		System.out.println(mvo.getLevel());
		List llist=service.getMyMentorLevel(mvo.getLevel());

		System.out.println(llist);
		return new ModelAndView("question.mentoring","llist",llist);
	}
	/**
	 * 멘토 레벨 선택 시  자신의 레벨과 비교하여 
	 * 소모되는 포인트를 계산하여 보여준다.
	 * @SSA
	 * @param level
	 * @param session
	 * @return
	 * @throws Exception
	 */ 
	@RequestMapping("/mentoring/getPointForRequest.do")
	@ResponseBody
	public int getPointForRequestAjax(int level, HttpSession session) throws Exception{
		Map map=new HashMap();
		System.out.println(level);
		map.put("ulevel", level);
		map.put("mylevel", ((MemberVO)session.getAttribute("mvo")).getLevel());
		//System.out.println(point); 
		return service.getPointForRequest(map);
	}
	/**
	 * 선택한 레벨의 해당하는 임의의 멘토에게 질문을 전송한다.
	 * 레벨 별 해당되는 포인트가 감소된다.
	 * BY SSA
	 * @throws Exception  
	 * 
	 */
	@RequestMapping("/mentoring/sendMentorQuestion.do")
	public String sendMentorQuestion(NoteVO nvo, HttpSession session) throws Exception{
		MemberVO mvo=(MemberVO)session.getAttribute("mvo");
		nvo.setSender(mvo);
		service.sendMentorQuestion(nvo);
		return "redirect:status.do";
	}
	
	@RequestMapping("/mentoring/rejectQuestion.do")
	public String rejectQuestion(int no) throws Exception{
		service.rejectMentorQuestion(no);
		return "redirect:status.do";

	}
	
	@RequestMapping("/mentoring/setReceiveDate.do")
	public void setReceiveDate(int no) throws Exception{
		service.setReceiveDate(no);

	}
	
	   /**
	    * 답장 팝업창을 보여준다.
	    * by ssa
	 * @throws Exception 
	    *     
	    */
	   @RequestMapping("/mentoring/replyView.do")
		public ModelAndView replyView(int no) throws Exception
		{
			NoteVO nvo=service.viewNoteContent(no);
			//System.out.println(nvo);
			return new ModelAndView("/WEB-INF/view/mentoring/mentoring_reply_popup","nvo",nvo);
		}
	   @RequestMapping("/mentoring/replyMentorQuestion.do")
	   public ModelAndView replyMentorQuestion(NoteVO nvo,HttpSession session) throws Exception{
		   System.out.println("!!!!"+nvo);
		   service.replyMentorQuestion(nvo);
		   return new ModelAndView("redirect:status.do"); 
	   }
}
